var windowName = "myTaskPayHire";
var Page,count = 0;
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show")
        if (count > 0) {

        }
        count++;

    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        id:0,
        money:0,
        payType:100,
    },{
        //页面方法
        "init":function () {
            var data = getData4Page();
            logJson(data)
            this.id = data.id
            this.money = data.money
        }
        ,doPay:function () {
            apiGet("/app/pay/task/hire",{id:Page.id ,type:Page.payType},function (res,code) {
                log(res)
                log(code,"code")
                if (Page.payType == 100){
                    //微信支付
                    wxPay(res.data,function (ret) {
                        logJson(ret,"PAY_RET")
                        checkPay()
                    })
                }
                if (Page.payType == 200) {
                    //支付宝支付
                    aliPay(res.data,function (ret,err) {
                        logJson(ret,"ali_ret")
                        logJson(err,"ali_err")
                        if (ret.code == 6001){
                            showToast("支付取消")
                        }else {
                            checkPay()
                        }
                    });
                }
                if (Page.payType == 500) {
                    //余额支付
                    checkPay()
                }
            })
        }
    })
    Page.init()
}

function checkPay() {
    showLoading();
    apiGet("/app/pay/task/check",{id:Page.id},function (res,code) {
        clearLoading();
        if (code == 200 && res.data == 1) {
            showToast("支付完成");
            goback();
        }else {
            setInterval(function () {
                apiGet("/app/pay/task/check",{id:Page.id},function (res,code) {
                    if (code == 200 && res.data == 1) {
                        showToast("支付完成");
                        goback();
                    }else {
                    }
                });
            },1000)
        }
    });
}